package com.wangyadong.hobby.schedule.jobtask.thread;


import java.util.concurrent.TimeUnit;

public class VolatileTest2 {

    private static volatile int INIT_VALUE = 0;

    private final static int MAX_LIMIT = 100;

    public static void main(String[] args) {
        example1();
    }


    private static void example1() {
        new Thread(() -> {
            int localValue = INIT_VALUE;
            while (localValue < MAX_LIMIT) {
                if (INIT_VALUE != localValue) {
                    System.out.println(" ---t1=" + Thread.currentThread().getName() + "--->" + INIT_VALUE);
                    localValue = INIT_VALUE;
                }
            }
        }, "reader").start();

        new Thread(() -> {
            int localValue = INIT_VALUE;
            while (INIT_VALUE < MAX_LIMIT) {
                System.out.println(Thread.currentThread().getName() + " ---t2=" + (++localValue));
                INIT_VALUE = localValue;
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "update").start();
    }
}
